<?php
	class sanphamdb
	{
		public function Add($MaLoai, $MaNhaSX, $MaNhomHang, $TenSanPham, $MoTa, $DonGia, $SoLuong, $NgayDang, $LuotXem)
		{
			$sql = " INSERT INTO `tbl_sanpham`(`MaNhomHang`, `MaLoai`, `MaNhaSX`, `TenSanPham`, `MoTa`, `DonGia`, `SoLuong`, `LuotXem`, `NgayDang`) VALUES ($MaNhomHang, $MaLoai, $MaNhaSX, '$TenSanPham', '$MoTa', $DonGia, $SoLuong, $LuotXem, '$NgayDang' ) ";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function Delete($MaSanPham)
		{
			$sql = "DELETE FROM `tbl_sanpham` WHERE MaSanPham = '$MaSanPham'";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		
		public function Update($MaSanPham, $MaLoai, $MaNhaSX, $MaNhomHang, $TenSanPham, $MoTa, $DonGia, $SoLuong)
		{
			$sql = "UPDATE `tbl_sanpham` SET `MaLoai` = '$MaLoai', `MaNhaSX` = '$MaNhaSX', `MaNhomHang`= '$MaNhomHang', `TenSanPham` = '$TenSanPham',`MoTa`='$MoTa',`DonGia` = $DonGia, `SoLuong` = $SoLuong WHERE MaSanPham= '$MaSanPham' "; 
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		public function Update_SoLuong($MaSanPham, $SoLuong)
		{
			$sql = "UPDATE `tbl_sanpham` SET `SoLuong`=$SoLuong WHERE MaSanPham= '$MaSanPham'"; 
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}
		public function AscNumView($MaSanPham)
		{
			$sql = "UPDATE `tbl_sanpham` SET `LuotXem` = `LuotXem` + 1 WHERE `MaSanPham` = $MaSanPham";
			$data = new dataservice();
			return $data->ExecuteNonQuery($sql);
		}

		public function GetList($MaSanPham, $MaLoai, $MaNhaSX, $MaNhomHang, $TenSanPham,
		 $MoTa, $DonGia, $SoLuong)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai  AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang ";
			if($MaSanPham!="")
				$sql .= " AND tbl_sanpham.MaSanPham = '$MaSanPham' ";
			if($MaLoai!="")
				$sql .= " AND tbl_sanpham.MaLoai = $MaLoai ";
			if($MaNhaSX!="")
				$sql .= " AND tbl_sanpham.MaNhaSX = $MaNhaSX ";
			if($MaNhomHang!="")
				$sql .= " AND tbl_sanpham.MaNhomHang = $MaNhomHang ";
			if($TenSanPham!="")
				$sql .= " AND tbl_sanpham.TenSanPham LIKE '%$TenSanPham%' ";
			if($MoTa!="")
				$sql .= " AND tbl_sanpham.MoTa LIKE '%$MoTa%' ";
			if($DonGia!="")
				$sql .= " AND tbl_sanpham.DonGia = $DonGia ";
			if($SoLuong!="")
				$sql .= " AND tbl_sanpham.SoLuong = $SoLuong ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function GetPageList($MaSanPham, $MaLoai, $MaNhaSX, $MaNhomHang, $TenSanPham,
		 $MoTa, $DonGia, $SoLuong, $start, $limit)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai  AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang ";
			if($MaSanPham!="")
				$sql .= " AND tbl_sanpham.MaSanPham = '$MaSanPham' ";
			if($MaLoai!="")
				$sql .= " AND tbl_sanpham.MaLoai = $MaLoai ";
			if($MaNhaSX!="")
				$sql .= " AND tbl_sanpham.MaNhaSX = $MaNhaSX ";
			if($MaNhomHang!="")
				$sql .= " AND tbl_sanpham.MaNhomHang = $MaNhomHang ";
			if($TenSanPham!="")
				$sql .= " AND tbl_sanpham.TenSanPham LIKE '%$TenSanPham%' ";
			if($MoTa!="")
				$sql .= " AND tbl_sanpham.MoTa LIKE '%$MoTa%' ";
			if($DonGia!="")
				$sql .= " AND tbl_sanpham.DonGia = $DonGia ";
			if($SoLuong!="")
				$sql .= " AND tbl_sanpham.SoLuong = $SoLuong ";
			$sql .= " LIMIT $start, $limit ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		
		public function GetList_New()
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai  AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang ORDER BY tbl_sanpham.MaSanPham DESC ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		public function GetPageList_New($start, $limit)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai  AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang ORDER BY tbl_sanpham.MaSanPham DESC LIMIT $start, $limit";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		public function GetList_MaSanPham($dsma)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai  AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang AND MaSanPham in ($dsma)";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		public function Find($key, $loai)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai  AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang " ;
			if($loai == 0) $sql.= " AND tbl_sanpham.TenSanPham LIKE '%$key%' ";
			else if($loai == 1) $sql.= " AND tbl_nhasanxuat.TenNhaSX like '%$key%' ";
			else if($loai == 2) $sql.= " AND tbl_loaisp.TenLoai LIKE '%$key%' ";
			else if($loai == 3) $sql.= " AND tbl_nhomhang.TenNhomHang LIKE '%$key%' ";
			$sql.= " GROUP BY tbl_sanpham.MaSanPham ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
		public function DS_Find($key, $loai, $start, $limit)
		{
			$sql = " SELECT * FROM tbl_sanpham, tbl_loaisp, tbl_nhasanxuat, tbl_nhomhang WHERE tbl_sanpham.MaLoai = tbl_loaisp.MaLoai  AND tbl_sanpham.MaNhaSX = tbl_nhasanxuat.MaNhaSX AND tbl_sanpham.MaNhomHang = tbl_nhomhang.MaNhomHang " ;
			if($loai == 0) $sql.= " AND tbl_sanpham.TenSanPham LIKE '%$key%' ";
			else if($loai == 1) $sql.= " AND tbl_nhasanxuat.TenNhaSX like '%$key%' ";
			else if($loai == 2) $sql.= " AND tbl_loaisp.TenLoai LIKE '%$key%' ";
			else if($loai == 3) $sql.= " AND tbl_nhomhang.TenNhomHang LIKE '%$key%' ";
			$sql.= " GROUP BY tbl_sanpham.MaSanPham LIMIT $start, $limit ";
			$data = new dataservice();
			return $data->ExecuteQuery($sql);
		}
	}
?>